﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class FrelController : Controller
    {
        //
        // GET: /Frel/
        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSQHGD
                            where dt.IDCanBo == id
                            select dt).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.QuanHe,
                                 customer.HoVaTen,
                                 customer.NamSinh,
                                 customer.QueQuan,
                                 customer.NgheNghiep,
                                 customer.ChucVu,
                                 customer.DonViCongTac,
                                 customer.DiaChi,
                                 customer.GhiChu
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index(int id = 0)
        {

            List<QHGD> dsqhgd = QHGiaDinhModels.GetDSQHGDTheoCanBo(id);
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            ChucVu cv = PosModels.GetChiTietDSChucVu((int)cb.ChucVuHienTai);
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(cb.IDPhongBan);
            ViewBag.ChucVu = cv;
            ViewBag.PhongBan = pb;
            ViewBag.DSQHGD = dsqhgd;
            ViewBag.ChiTietCanBo = cb;
           
            //ViewBag.idCB = Request.Params["id"];
            return View(dsqhgd);

           
            
        }
        public ActionResult Add(int id=0)
        {

            ViewBag.Id = id;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form,int id)
        {
            QHGD themQHGD = new QHGD();
            themQHGD.QuanHe = form["QuanHe"];
            themQHGD.HoVaTen = form["HoVaTen"];
            themQHGD.NamSinh = form["NamSinh"];
            themQHGD.QueQuan = form["QueQuan"];
            themQHGD.NgheNghiep = form["NgheNghiep"];
            themQHGD.ChucVu = form["ChucVu"];
            themQHGD.DonViCongTac = form["DonViCongTac"];
            themQHGD.DiaChi = form["DiaChi"];
            themQHGD.GhiChu = form["GhiChu"];

            themQHGD.IDCanBo = id;
 
            
            QHGiaDinhModels.Add(themQHGD);

            return RedirectToAction("Index", "Frel", new { id = themQHGD.IDCanBo });
        }
        public ActionResult Update(int id)
        {
            QHGD listQHGD = QHGiaDinhModels.GetChiTietDSQHGD(id);
            ViewBag.DSQHGD = listQHGD;
            

            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {

            QHGD s = QHGiaDinhModels.GetChiTietDSQHGD(id);
            //dt.STT = stt;
            s.QuanHe = form["QuanHe"];
            s.HoVaTen = form["HoVaTen"];
            s.NamSinh = form["NamSinh"];
            s.QueQuan = form["QueQuan"];
            s.NgheNghiep = form["NgheNghiep"];
            s.ChucVu = form["ChucVu"];
            s.DonViCongTac = form["DonViCongTac"];
            s.DiaChi = form["DiaChi"];
            s.GhiChu = form["GhiChu"];

            QHGiaDinhModels.Update(s);
            return RedirectToAction("Index", "Frel", new { id = s.IDCanBo });
        }
        public ActionResult Delete(int id)
        {
            QHGD s = QHGiaDinhModels.GetChiTietDSQHGD(id);
            QHGiaDinhModels.Delete(id);

            return RedirectToAction("Index", "Frel", new { id = s.IDCanBo});
        }

    }
}
